package com.habbot.beautifyinghandbook.models;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.habbot.beautifyinghandbook.daos.DBAdapter;
import com.habbot.beautifyinghandbook.objects.ItemObject;

public class ItemTable {
	public static final String TB_NAME = "ZITEM";
	public static final String COL_ID = "Z_PK", COL_ENT = "Z_ENT",
			COL_OPT = "Z_OPT", COL_MENU = "ZMENU", COL_TITLE = "ZTITLE",
			COL_CONTENT = "ZCONTENT", COL_LINK = "ZLINK", COL_FOCUS = "ZFOCUS";

	private DBAdapter dbAdapter;

	public ItemTable(Context context) {
		dbAdapter = DBAdapter.getInstance(context);
	}

	public ItemObject getItemById(int id){
		try{
			SQLiteDatabase db=dbAdapter.openDataBase();
			Cursor cursor=db.rawQuery("SELECT * FROM "+TB_NAME+" WHERE "+COL_ID+"="+id , null);
			if(cursor.moveToNext()){
				ItemObject item=new ItemObject();
				item.setId(cursor.getInt(cursor.getColumnIndex(COL_ID)));
				item.setEnt(cursor.getInt(cursor.getColumnIndex(COL_ENT)));
				item.setOpt(cursor.getInt(cursor.getColumnIndex(COL_OPT)));
				item.setMenuId(cursor.getInt(cursor.getColumnIndex(COL_MENU)));
				item.setTitle(cursor.getString(cursor.getColumnIndex(COL_TITLE)));
				item.setContent(cursor.getString(cursor.getColumnIndex(COL_CONTENT)));
				item.setLink(cursor.getString(cursor.getColumnIndex(COL_LINK)));
				item.setFocus(cursor.getInt(cursor.getColumnIndex(COL_FOCUS)));
				db.close();
				return item;
			}
			db.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return null;
	}
	public ItemObject getItemById(String id){
		try{
			SQLiteDatabase db=dbAdapter.openDataBase();
			Cursor cursor=db.rawQuery("SELECT * FROM "+TB_NAME+" WHERE "+COL_ID+"="+id , null);
			if(cursor.moveToNext()){
				ItemObject item=new ItemObject();
				item.setId(cursor.getInt(cursor.getColumnIndex(COL_ID)));
				item.setEnt(cursor.getInt(cursor.getColumnIndex(COL_ENT)));
				item.setOpt(cursor.getInt(cursor.getColumnIndex(COL_OPT)));
				item.setMenuId(cursor.getInt(cursor.getColumnIndex(COL_MENU)));
				item.setTitle(cursor.getString(cursor.getColumnIndex(COL_TITLE)));
				item.setContent(cursor.getString(cursor.getColumnIndex(COL_CONTENT)));
				item.setLink(cursor.getString(cursor.getColumnIndex(COL_LINK)));
				item.setFocus(cursor.getInt(cursor.getColumnIndex(COL_FOCUS)));
				db.close();
				return item;
			}
			db.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return null;
	}
}
